<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>万年历</title>
    <link rel="stylesheet" href="../bzstyle.css">
    <style>
        .time{
            width: 350px;
            height: 400px;
            margin: 0 auto;
            border:1px dotted #ddd;
        }
        .time .head1{
            height: 20px;
            width: 100%;
            background-color: #e7eef8;
        }
        .time .head1 p{
            width: 210px;
            margin: 0 auto;
            font-family: 'Kaiti';
        }
        select{
            width: 60px;
        }
        .time .head2{
            height: 30px;
            width: 100%;
        }
        .time .head2 ul li{
            width: 48px;
            height: 28px;
            float: left;
            text-align: center;
            line-height: 30px;
            border: 1px solid rgb(0, 41, 128);
        }
        .time .footer{
            clear: both;  /*清除浮动*/
        }
        .time .footer .day{
            width: 48px;
            height: 48px;
            border: 1px solid rgb(0, 140, 255);
            float: left;
            text-align: center;
            line-height: 48px;
            font-weight: bold;
        }
        .time .cur{
            width: 100%;
            clear: both;
        }
        .time .cur>div{
            width: 180px;
            height: 30px;
            margin: 0px auto;
            color: red;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div class="time">
        <div class="head1">
            <p>公元
                <select name="year" id="year" onchange="ck2()"></select>
                年
                <select name="month" id="month" onchange="ck2()"></select>月
            </p>
        </div>
        <div class="head2">
            <ul>
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
                <li>6</li>
                <li>7</li>
            </ul>
        </div>
        <div class="footer">
        </div>
        <div class="cur">
            <div>现在是：
                <span id="h"></span>:
                <span id="m"></span>:
                <span id="s"></span>
            </div>
        </div>
    </div>
</body>
<script>
    var year1=document.getElementById('year');
    var month1=document.getElementById('month');
    var footer=document.getElementsByClassName('footer')[0];

    var date=new Date();
    var zYear=date.getFullYear();
    var zMonth=date.getMonth();
    var zDay=date.getDate();   // 获取当前多少号
    var zX=date.getDay();   // 获取当前周几

    for(var i=1900;i<=2050;i++){
        var ele=createEle('option',i);
        ele.innerHTML=i;
        if(i==zYear){
            ele.selected=true;
        }
        year1.appendChild(ele);
    }// 通过js代码动态生成年份下拉框

    for(var i=1;i<13;i++){
        var ele=createEle('option',i);
        ele.innerHTML=i;
        if((i-1)==zMonth){
            ele.selected=true;
        }
        month1.appendChild(ele);
    }// 通过js代码动态生成月份下拉框


    function is_Date(year1,month1){
        var date2=new Date(year1,month1,1,0,0);
        console.log(date2);
        var day=date2.getDay();
        return day==0?7:day;
    }// 函数，用于获取当前月份的1号对应的星期


    function ck2(){
        footer.innerHTML='';
        var hao_1=is_Date(year1.value,month1.value-1);
        var days=panduan(year1.value,month1.value-1);
        console.log(days+" "+hao_1);
        for(var i=1;i<hao_1;i++){
            var ele=createEle('div','');
            ele.className='day';    // 设置标签元素的class属性值
            footer.appendChild(ele);
        }

        for(var i=1;i<=days;i++){
            var ele=createEle('div','');
            ele.className='day';
            ele.innerHTML=i;
            if(year1.value==zYear && month1.value-1==zMonth && i==zDay){
                ele.style.background="rgb(193, 217, 255)";
            }
            footer.appendChild(ele);
        }
    }

    function panduan(year,month){
        var days=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
        if(year%400==0 ||(year%4==0 && year%100!=0)){// 闰年
            days[1]=29;
        }
        return days[month];
    }// 获取某年某月的天数

    function createEle(eleName,value){
        var ele=document.createElement(eleName);
        ele.value=value;
        return ele;
    }
    ck2();  // 默认情况
    setInterval(function(){
        var date1=new Date();
        var hour=date1.getHours();
        var minute=date1.getMinutes();
        var second=date1.getSeconds();
        document.getElementById('h').innerHTML=("0"+hour).length>2?hour:"0"+hour;
        document.getElementById('m').innerHTML=("0"+minute).length>2?minute:"0"+minute;
        document.getElementById('s').innerHTML=("0"+second).length>2?second:"0"+second;
    },1000);
</script>
</html>